'use strict'

import Post from "../models/Post.js"
import FileService from "../service/FileService.js"

class PostService {

  async create(post, picture) {
    const newPicture = FileService.saveFile(picture)
    const newPost = Post.create({...post, picture: newPicture})
    return newPost
  }

  async getAll() {
    const posts = await Post.find()
    return posts
  }

  async getOne(id) {
    const post = await Post.findById(id)
    return post
  }

  async update(id, post) {
    const updatedPost = await Post.findByIdAndUpdate(id, post, {
      new: true
    })
    return updatedPost
  }

  async delete(id) {
    const post = await Post.findByIdAndDelete(id)
    return post
  }

  async deleteAll() {
    const posts = await Post.deleteMany()
    return posts
  }

}

export default new PostService()